import urllib.request
import urllib.error
import re
file = open("D:/58home.txt","w")
for page in range(1,11):
    url = 'http://bj.58.com/dashanzi/chuzu/pn'+str(page)+'/?PGTID=0d3090a7-01d7-f30a-8081-96ffca688ebd&ClickID=1'
    try:
        content = urllib.request.urlopen(url).read().decode("utf-8", "ignore")
        pat = '<h2>.*?<a.*?target="_blank" >(.*?)</a>.*?</h2>.*?<p class="room">(.*?)</p>.*?<p class="add">.*?<div class="money">.*?<b>(.*?)</b>元/月.*?<img.*?lazy_src="(.*?)".*?src=".*?">'
        result = re.compile(pat,re.S).findall(content)
        for home in result:
            title = home[0].strip()
            huxing = home[1].replace(" ","").replace("&nbsp;","")
            line = "标题："+title+"  户型："+huxing+"  价格："+home[2]+"元/月"+"  图片："+home[3]+'\n'
            file.write(line)
    except Exception as err:
        if hasattr(err,"code"):
            print("HTTPError")
            print(err.code+"-----"+err.reason)
        if hasattr(err,"reason"):
            print("URLError")
            print(err.reason)
file.close()
print("写入成功！！！")
